<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    class="flex"
                    :disabled="baTable.form.operate=='view'"
                >
                <FormItem :label="t('xSecurity.CORP')" type="string" v-model="baTable.form.items!.CORP" prop="CORP" :input-attr="{ placeholder: t('Please input field', { field: t('xSecurity.CORP') }), disabled: true}" />
                <FormItem :label="t('xSecurity.SJDATE')" type="datetime" v-model="baTable.form.items!.SJDATE" prop="SJDATE" :input-attr="{ placeholder: t('Please input field', { field: t('xSecurity.SJDATE') }) }" />
                <FormItem :label="t('xSecurity.SJR')" type="string" v-model="baTable.form.items!.SJR" prop="SJR" :input-attr="{ placeholder: t('Please input field', { field: t('xSecurity.SJR') }) }" />
                <FormItem :label="t('xSecurity.ZGR')" type="string" v-model="baTable.form.items!.ZGR" prop="ZGR" :input-attr="{ placeholder: t('Please input field', { field: t('xSecurity.ZGR') }) }" />
                <FormItem style="width:100%" :label="t('xSecurity.ZGX')" type="string" v-model="baTable.form.items!.ZGX" prop="ZGX" :input-attr="{ placeholder: t('Please input field', { field: t('xSecurity.ZGX') }) }" />
                <FormItem :label="t('xSecurity.ZGDATE')" type="datetime" v-model="baTable.form.items!.ZGDATE" prop="ZGDATE" :input-attr="{ placeholder: t('Please input field', { field: t('xSecurity.ZGDATE') }) }" />
                <el-form-item :label="t('xSecurity.FINISHED')" prop="FINISHED" >
                    <el-switch
                        v-model="baTable.form.items!.FINISHED"
                        style="--el-switch-on-color: #13ce66;"
                        active-value="1"
                        inactive-value="0"
                        :before-change="handleFinishedChange"
                        :disabled="baTable.form.items!.FINISHED == '1'"
                    />
                </el-form-item>
                <!-- <FormItem :label="t('xSecurity.filename')" type="string" v-model="baTable.form.items!.filename" prop="filename" :input-attr="{ placeholder: t('Please input field', { field: t('xSecurity.filename') }) }" /> -->
                <FormItem :label="t('xSecurity.OP')" type="string" v-model="baTable.form.items!.OP" prop="OP" :input-attr="{placeholder: '当前登录用户',disabled:true }" />
                <FormItem :label="t('xSecurity.OPTIME')" type="datetime" v-model="baTable.form.items!.OPTIME" prop="OPTIME" :input-attr="{ placeholder: '当前日期',disabled:true }" />
                <FormItem style="width:100%" :label="t('xSecurity.ZGJL')" type="textarea" v-model="baTable.form.items!.ZGJL" prop="ZGJL" :input-attr="{ placeholder: t('Please input field', { field: t('xSecurity.ZGJL') }) }" />
                <FormItem style="width:100%" :label="t('xSecurity.DEMO')" type="textarea" v-model="baTable.form.items!.DEMO" prop="DEMO" :input-attr="{ placeholder: t('Please input field', { field: t('xSecurity.DEMO') }) }" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="handleSave" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject ,watchEffect} from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import { ElForm, ElMessageBox, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { useRoute } from 'vue-router'

const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    OPTIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xSecurity.OPTIME') })})],
})

const route = useRoute()
const type = route.name === 'corplib/customer'? 1:2

const companyName = inject('companyName') as String
watchEffect(()=>{
    if(baTable.form.operate == 'add'){
        baTable.form.items!.CORP = companyName.value
        baTable.form.items!.VALIDORNOT = '1'
    }
})

const handleSave= ()=>{
    baTable.form.items!.type = type
    baTable.onSubmit(formRef.value)
}

const handleFinishedChange = async()=>{
    try{
        await ElMessageBox.confirm(
            '确认完成后将无法修改，请确认？',
            "",
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
        return true
    } catch(err){
        return false
    }

}


</script>

<style scoped lang="scss"></style>
